package com.aptech.test;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ClearCookies extends HttpServlet {

//    ServletConfig sc;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        getServletConfig().getServletContext().setAttribute("test", "aaaaaaaaa");
        
        Cookie[] cs = request.getCookies();
        if (cs != null) {
            for (int i = 0; i < cs.length; i++) {
                Cookie c1 = cs[i];

                if (c1.getName().equals("username") || c1.getName().equals("password")) {
                    c1 = new Cookie(c1.getName(), null);
                    c1.setMaxAge(0);
                    c1.setPath("/");
                    response.addCookie(c1);
                }
            }
        }

        request.getSession().removeAttribute("username");
        request.getSession().removeAttribute("password");

        request.setAttribute("message", "Cookies username and password have been removed successfully.");
        request.getRequestDispatcher("/testCookie.jsp").forward(request, response);
    }

//    @Override
//    public void init(ServletConfig config) throws ServletException {
//        sc = config;
//    }


}
